import os
import sys

# 添加项目根目录到 Python 路径
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(project_root)

from apps import create_app
from apps.users.models import User
from common.query_filter import QueryFilter, ConditeOperator

app = create_app(is_cli=True)

# 在应用上下文中执行查询
with app.app_context():
    # 简单查询
    u = User(name='test3', age=20,  email='test3@example.com')
    u.password = "test123"
    u.add()
    # query = QueryFilter(User.query).fuzzy_match(["name", ], 'test').apply_filters(QueryFilter.Operator._or.value)
    # users = query.all()
    # print("简单查询结果:", [user.to_dict() for user in users])
    # u.delete()


    # 复杂组合查询
    # query = (QueryFilter(User.query)
    #     .exact_match(**{'age': 21})
    #     .fuzzy_match(['name'], 'test')  # 修改为实际的字段名
    #     .apply_filters(ConditeOperator._or.value))
    # users = query.all()
    # print("复杂查询结果:", [[user.to_dict(), user.password_hash] for user in users])